package com.tsingtree.modular.controller.view;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.base.auth.annotion.Permission;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.*;
import com.tsingtree.modular.model.params.NanSellerParam;
import com.tsingtree.modular.service.MercUserService;
import com.tsingtree.modular.service.MercUserService;
import com.tsingtree.modular.service.NanSellerService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import com.tsingtree.sys.core.constant.state.ManagerStatus;
import com.tsingtree.sys.core.util.SaltUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 运营组控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanSeller")
public class NanSellerController extends BaseController {

    private String PREFIX = "/nanSeller";

    @Autowired
    private NanSellerService nanSellerService;
    @Autowired
    private MercUserService mercUserService;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanSeller.html";
    }
    @RequestMapping("/choose")
    public String choose() {
        return PREFIX + "/nanSellerChoose.html";
    }
    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanSeller_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanSeller_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jagger
     */
    @RequestMapping("/addItem")
    @Permission
    @ResponseBody
    public ResponseData addItem(NanSellerParam nanSellerParam) {
        String phone = nanSellerParam.getPhone();
        if(phone.length() < 6){
            return ResponseData.error("手机号不正确");
        }
        int count = mercUserService.count(new QueryWrapper<MercUser>().eq("account", phone));
        if (count > 0) {
            return ResponseData.error("该手机号已被添加");
        }
        // 完善账号信息
        String salt = SaltUtil.getRandomSalt();
        String password = SaltUtil.md5Encrypt(phone.substring(phone.length() - 6), salt);
        MercUser newUser = new MercUser();
        newUser.setCreateTime(new Date());
        newUser.setStatus(ManagerStatus.OK.getCode());
        newUser.setAvatar("/static/images/avatar.png");
        newUser.setAccount(phone);
        newUser.setName(nanSellerParam.getName());
        newUser.setPassword(password);
        newUser.setSalt(salt);
        newUser.setIsAdmin(true);
        //创建角色
        MercRole mercRole = new MercRole();
        mercRole.setDescription("administrator");
        mercRole.setName("超级管理员");
        mercRole.setPid(0L);
        mercRole.setSort(0);
        this.nanSellerService.add(nanSellerParam, newUser, mercRole);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jagger
     */
    @RequestMapping("/editItem")
    @Permission
    @ResponseBody
    public ResponseData editItem(NanSellerParam nanSellerParam) {
        NanSeller nanSeller = nanSellerService.getById(nanSellerParam.getId());
        List<MercUser> updateMercUsers = new ArrayList<>();
        if(nanSeller.getIsForbid() != (nanSellerParam.getIsForbid()? 1: 0)){
            //需要修改登录账号的禁用状态
            List<MercUser> mercUserList = mercUserService.list(new QueryWrapper<MercUser>().eq("tenant_id", nanSeller.getId())
                    .orderByAsc("create_time"));
            mercUserList.forEach(mercUser -> {
                if(nanSellerParam.getIsForbid()){
                    mercUser.setStatus(ManagerStatus.FREEZED.getCode());
                }else{
                    mercUser.setStatus(ManagerStatus.OK.getCode());
                }
            });
            if(!CollectionUtils.isEmpty(mercUserList)){
                if(nanSellerParam.getIsForbid()){
                    //禁用的话所有账号禁用
                    updateMercUsers.addAll(mercUserList);
                }else{
                    //启用的话，启用主账号
                    updateMercUsers.add(mercUserList.get(0));
                }
            }
        }
        this.nanSellerService.update(nanSellerParam, updateMercUsers);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    //@RequestMapping("/delete")
    //@Permission
    //@ResponseBody
    public ResponseData delete(NanSellerParam nanSellerParam) {
        this.nanSellerService.delete(nanSellerParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanSellerParam nanSellerParam) {
        NanSeller detail = this.nanSellerService.getById(nanSellerParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/list")
    public LayuiPageInfo list(NanSellerParam nanSellerParam) {
        return this.nanSellerService.findPageBySpec(nanSellerParam);
    }

}


